package com.shujia.flink.tf

import org.apache.flink.streaming.api.scala._

object Demo4KeyBy {
  def main(args: Array[String]): Unit = {

    val env: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment

    env.setParallelism(2)

    //延迟时间
    //如果超过了100毫秒数据没有达到32kb也会向下游发送数据
    env.setBufferTimeout(100)

    val ds: DataStream[String] = env.socketTextStream("master", 8888)

    val ds2: KeyedStream[(String, Int), String] = ds
      .map((_, 1))
      .keyBy(_._1)

    /**
      * keyBy 之后接map函数之后不能使用聚合函数
      *
      *
      * reduce: 对数据进行聚合
      */

    val countDS: DataStream[(String, Int)] = ds2.reduce((kv1, kv2) => (kv2._1, kv1._2 + kv2._2))


    countDS.print()


    env.execute()
  }

}
